#include <stdio.h>
#include <string.h>

#define BUFSIZE	32

void string_to_lower(char *buf);
int mycpy(FILE *rfp, FILE *wfp);
int main(int argc, char **argv)
{
	FILE *fp1, *fp2;

	// argv[1]文件复制到argv[2]
	if (argc < 3)
		return 1;

	fp1 = fopen(argv[1], "r");
	if (NULL == fp1) {
		fprintf(stderr, "fopen():%s failed", argv[1]);
		return 1;
	}

	fp2 = fopen(argv[2], "w");
	if (NULL == fp2) {
		fclose(fp1);
		fprintf(stderr, "fopen():%s failed", argv[2]);
		return 1;
	}

	mycpy(fp1, fp2);

	fclose(fp1);
	fclose(fp2);

	return 0;
}

int mycpy(FILE *rfp, FILE *wfp)
{
	int c;

	while (1) {
		c = fgetc(rfp);
		if (EOF == c) {
			// 出错 结束标志
			break;
		}
		fputc(c, wfp);
	}

	return 0;
}

// 每次读写一行 fgets(3) fputs(3)
int mycpy_line(FILE *rfp, FILE *wfp)
{
	char buf[BUFSIZE] = {0};

	while (1) {
		memset(buf, 0, BUFSIZE);
		if (NULL == fgets(buf, BUFSIZE, rfp))
			break;
		string_to_lower(buf);
		fputs(buf, wfp);
	}

	return 0;
}


// 结构化读写 fread(3) fwrite(3)
int mycpy_struct(FILE *rfp, FILE *wfp)
{
	char buf[BUFSIZE] = {0};
	int cnt;

	while (1) {
		cnt = fread(buf, 1, BUFSIZE, rfp);	
		if (EOF == cnt)
			break;
		fwrite(buf, 1, cnt, wfp);
	}

	return 0;
}

void string_to_lower(char *buf)
{
	while (*buf) {
		if (*buf >= 'a' && *buf <= 'z')
			*buf = *buf - ('a' - 'A');
		else if (*buf >= 'A' && *buf <= 'Z')
			*buf = *buf + ('a' - 'A');
		buf++;
	}
}




